package cn.com.qiancheng.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.com.qiancheng.dto.AdminDto;
import cn.com.qiancheng.util.DbUtil;

public class AdminDao {
	public AdminDto searchAdminByUserName(String userName) {
		AdminDto admin = null;
		try {
			Connection con = DbUtil.openConnection();
			String sql = "SELECT ad_id,ad_name," 
						+ "ad_password," 
						+ "ad_realname " 
						+ "FROM t_admin WHERE "
						+ "ad_name =?";
			PreparedStatement stat = con.prepareStatement(sql);
			stat.setString(1, userName);
			ResultSet rs = stat.executeQuery();
			if (rs.next()) {
				admin = new AdminDto();
				admin.setAdminId(rs.getInt("ad_id"));
				admin.setPassword(rs.getString("ad_password"));
				admin.setUserName(rs.getString("ad_name"));
				admin.setRealName(rs.getString("ad_realname"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				DbUtil.closeConnection();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return admin;
	}
}
